package com.yupi.yurpc.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

@Slf4j
public class MockServiceProxy implements InvocationHandler {

    /*.
     *调用代理
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //根据方法的返回值类型，生成指定的默认值对象
        Class<?> methodReturnType = method.getReturnType();
        log.info("mock invoke {}",method.getReturnType());
        return getDefaultObject(methodReturnType);



    }

    /**
     * 生成指定类型的默认值对象(可自行完善默认值逻辑)
     * @param methodReturnType
     * @return
     */
    private Object getDefaultObject(Class<?> methodReturnType) {
        //基本类型
        if(methodReturnType.isPrimitive()){
            if(methodReturnType==boolean.class){
                return false;
            }else if(methodReturnType==short.class){
                return (short)0;
            }else if(methodReturnType==int.class){
                return 0;
            }else if(methodReturnType==long.class){
                return 0L;
            }
        }
        //对象类型
        return null;

    }
}
